﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;
using Microsoft.DirectX.DirectInput;

namespace NGE
{
    public class Keyboard
    {
        public Keyboard(Game g)
        {
            kdev = new Microsoft.DirectX.DirectInput.Device(Microsoft.DirectX.DirectInput.SystemGuid.Keyboard);
            kdev.Acquire();
            game = g;
            ks = new KeyboardState[256];

        }
        public void Dispose()
        {
            kdev.Unacquire();
            kdev.Dispose();
        }
        Device kdev;
        Game game;
        KeyboardState[] ks;
        public KeyboardState this[int i]
        {
            get
            {
                return ks[i];
            }
        }
        public void Heartbeat()
        {
            if (game.Option.KeyboardHandleModel == KeyboardHandleMode.DirectInput)
            {
                Microsoft.DirectX.DirectInput.KeyboardState keyboardState = kdev.GetCurrentKeyboardState();
                for (int keycode = 0; keycode < ks.Length; keycode++)
                {
                    if (!keyboardState[(Key)keycode])
                    {
                        if (ks[keycode] != KeyboardState.KeyUp)
                        {
                            for (int i = 0; i < game.Components.Count; i++)
                            {
                                ks[keycode] = KeyboardState.KeyUp;
                                game.Components[i].NotifyKeyboardEvent(keycode, ks[keycode]);
                            }
                            //game.Script.NotifyKeyboardEvent(keycode, ks[keycode]);
                        }
                    }
                    else
                    {
                        if (ks[keycode] == KeyboardState.KeyUp)
                        {
                            for (int i = 0; i < game.Components.Count; i++)
                            {
                                ks[keycode] = KeyboardState.KeyDown;
                                game.Components[i].NotifyKeyboardEvent(keycode, ks[keycode]);
                            }
                            //game.Script.NotifyKeyboardEvent(keycode, ks[keycode]);
                        }
                        else
                        {
                            for (int i = 0; i < game.Components.Count; i++)
                            {
                                ks[keycode] = KeyboardState.KeyHold;
                                game.Components[i].NotifyKeyboardEvent(keycode, ks[keycode]);
                            }
                            //game.Script.NotifyKeyboardEvent(keycode, ks[keycode]);
                        }
                    }
                }
            }
            else if (game.Option.KeyboardHandleModel == KeyboardHandleMode.SystemMessage)
            {
                Message msg = game.SysMsg.msg;

                if (msg.Msg == 0x101)
                {
                    int keycode = (game.SysMsg.msg.LParam.ToInt32() >> 16) & 0xff;

                    if (ks[keycode] != KeyboardState.KeyUp)
                    {
                        for (int i = 0; i < game.Components.Count; i++)
                        {
                            ks[keycode] = KeyboardState.KeyUp;
                            game.Components[i].NotifyKeyboardEvent(keycode, ks[keycode]);
                        }
                        //game.Script.NotifyKeyboardEvent(keycode, ks[keycode]);
                    }
                }
                else if (msg.Msg == 0x100)
                {
                    int keycode = (game.SysMsg.msg.LParam.ToInt32() >> 16) & 0xff;

                    if (ks[keycode] == KeyboardState.KeyUp)
                    {
                        for (int i = 0; i < game.Components.Count; i++)
                        {
                            ks[keycode] = KeyboardState.KeyDown;
                            game.Components[i].NotifyKeyboardEvent(keycode, ks[keycode]);
                        }
                        //game.Script.NotifyKeyboardEvent(keycode, ks[keycode]);
                    }
                    else
                    {
                        for (int i = 0; i < game.Components.Count; i++)
                        {
                            ks[keycode] = KeyboardState.KeyHold;
                            game.Components[i].NotifyKeyboardEvent(keycode, ks[keycode]);
                        }
                        //game.Script.NotifyKeyboardEvent(keycode, ks[keycode]);
                    }
                }
            }
        }
    }
}
